{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b8547684",
   "metadata": {},
   "source": [
    "* 时间：2023-2-26\n",
    "* 学习课程：Python数据分析\n",
    "* 记录人：赖文佩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db1c4017",
   "metadata": {},
   "source": [
    "# jupyter notebook 键盘快捷键\n",
    "\n",
    "## 进入命令模式之后（此时你没有活跃单元），你可以尝试以下快捷键：\n",
    "* A 会在活跃单元之上插入一个新的单元，B 会在活跃单元之下插入一个新单元。\n",
    "* 连续按两次 D，可以删除一个单元。\n",
    "* 撤销被删除的单元，按 Z。\n",
    "* Y 会将当前活跃的单元变成一个代码单元。\n",
    "* 按住 Shift +上或下箭头可选择多个单元。在多选模式时，按住 Shift + M 可合并你的选择。\n",
    "* 按 F 会弹出「查找和替换」菜单。\n",
    "\n",
    "## 处于编辑模式时（在命令模式时按 Enter 会进入编辑模式），你会发现下列快捷键很有用：\n",
    "\n",
    "* Ctrl + Home 到达单元起始位置。\n",
    "* Ctrl + S 保存进度。\n",
    "* 如之前提到的，Ctrl + Enter 会运行你的整个单元块。\n",
    "* Alt + Enter 不止会运行你的单元块，还会在下面添加一个新单元。\n",
    "* Ctrl + Shift + F 打开命令面板。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7f1dbb7",
   "metadata": {},
   "source": [
    "# Python 基础回顾（预备知识）\n",
    "## 列表推导式与条件赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e9e33403",
   "metadata": {},
   "outputs": [],
   "source": [
    "student_index = [1000001,1000002,1000003,1000004,1000005,1000006]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a21a6061",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 需求： 学号 奇数先筛选出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f9d05d03",
   "metadata": {},
   "outputs": [],
   "source": [
    "奇数_students = []\n",
    "for i in student_index:\n",
    "    if i%2 != 0:\n",
    "        奇数_students.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "428482ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000001, 1000003, 1000005]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "奇数_students"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fd7dabdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000001, 1000003, 1000005]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表推导式 ---> [结果变量  for循环  循环体]  ==> 一切的循环都可以用推导式来写\n",
    "奇数_students = [i for i in student_index if i%2 !=0]\n",
    "奇数_students"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c5563ac3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a_c', 'a_d', 'b_c', 'b_d']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 课本的例子\n",
    "results = [m+'_'+n for m in['a','b'] for n in ['c','d']]\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "903370af",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = []\n",
    "for m in['a','b']:\n",
    "    for n in ['c','d']:\n",
    "        results.append(m+'_'+n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7102732f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a_c', 'a_d', 'b_c', 'b_d']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c456eab",
   "metadata": {},
   "source": [
    "# 文件的读取和写入（P27）\n",
    "* 1.csv            \n",
    "* 2.excel          \n",
    "* 3.text       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "744c3f64",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4ac2583e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>排名</td>\n",
       "      <td>排名变化</td>\n",
       "      <td>企业</td>\n",
       "      <td>价值（亿元人民币）</td>\n",
       "      <td>价值变化（亿元人民币）</td>\n",
       "      <td>总部</td>\n",
       "      <td>行业</td>\n",
       "      <td>成立年份</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>抖音</td>\n",
       "      <td>13400</td>\n",
       "      <td>-10050</td>\n",
       "      <td>北京</td>\n",
       "      <td>社交媒体</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>SpaceX</td>\n",
       "      <td>8400</td>\n",
       "      <td>1680</td>\n",
       "      <td>洛杉矶</td>\n",
       "      <td>航天</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>-1</td>\n",
       "      <td>蚂蚁集团</td>\n",
       "      <td>8000</td>\n",
       "      <td>-2010</td>\n",
       "      <td>杭州</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>Stripe</td>\n",
       "      <td>4100</td>\n",
       "      <td>-2230</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>11</td>\n",
       "      <td>Shein</td>\n",
       "      <td>4000</td>\n",
       "      <td>2680</td>\n",
       "      <td>广州</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>15</td>\n",
       "      <td>币安</td>\n",
       "      <td>3000</td>\n",
       "      <td>2010</td>\n",
       "      <td>马耳他</td>\n",
       "      <td>区块链</td>\n",
       "      <td>2017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>Databricks</td>\n",
       "      <td>2500</td>\n",
       "      <td>0</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>大数据</td>\n",
       "      <td>2013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>微众银行</td>\n",
       "      <td>2200</td>\n",
       "      <td>200</td>\n",
       "      <td>深圳</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>京东科技</td>\n",
       "      <td>2000</td>\n",
       "      <td>0</td>\n",
       "      <td>北京</td>\n",
       "      <td>数字科技</td>\n",
       "      <td>2013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>Checkout.com</td>\n",
       "      <td>1900</td>\n",
       "      <td>870</td>\n",
       "      <td>伦敦</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     0     1             2          3            4    5     6     7\n",
       "0   排名  排名变化            企业  价值（亿元人民币）  价值变化（亿元人民币）   总部    行业  成立年份\n",
       "1    1     0            抖音      13400       -10050   北京  社交媒体  2012\n",
       "2    2     1        SpaceX       8400         1680  洛杉矶    航天  2002\n",
       "3    3    -1          蚂蚁集团       8000        -2010   杭州  金融科技  2014\n",
       "4    4     0        Stripe       4100        -2230  旧金山  金融科技  2010\n",
       "5    5    11         Shein       4000         2680   广州  电子商务  2012\n",
       "6    6    15            币安       3000         2010  马耳他   区块链  2017\n",
       "7    7     1    Databricks       2500            0  旧金山   大数据  2013\n",
       "8    8     3          微众银行       2200          200   深圳  金融科技  2014\n",
       "9    9     2          京东科技       2000            0   北京  数字科技  2013\n",
       "10  10    11  Checkout.com       1900          870   伦敦  金融科技  2012"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html('https://hurun.net/zh-CN/Info/Detail?num=L9SQPH9FKJB1')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "35feb4d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.read_html('https://hurun.net/zh-CN/Info/Detail?num=L9SQPH9FKJB1')[0].to_excel('output_hurun.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24352e4a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
